﻿using System;

namespace Fuse8.DomainFramework.Domain
{
	[Serializable]
	public abstract class ComparableEntityObject<T> : IEntityObject<T> where T : IComparable<T>, IEquatable<T>
	{
		public virtual string TypeParameter
		{
			get
			{
				return GetType().FullName;
			}
		}

		public virtual string EntityIdentfier
		{
			get { return Id.ToString(); }
		}
		
		public virtual bool IsNew
		{
			get { return Id.Equals(default(T)); }
		}

		public virtual bool IsCreated
		{
			get
			{
				return !IsNew;
			}
		}

		public virtual T Id { get; set; }

		public abstract void IncreaseId();

		#region IEqualityComparer<IEntityObject<int>> Members
		public virtual bool Equals(IEntityObject<T> x, IEntityObject<T> y)
		{
			return (x != null) && (y != null) && x.GetType() == y.GetType() && x.Id.Equals(y.Id);
		}

		public virtual int GetHashCode(IEntityObject<T> obj)
		{
			return obj != null ?
				obj.Id.GetHashCode() :
				int.MinValue.GetHashCode();
		}
		#endregion

		public override bool Equals(object obj)
		{
			var entity = obj as IEntityObject<T>;
			return entity != null && entity.GetType() == GetType() && entity.Id.Equals(Id);
		}

		public override int GetHashCode()
		{
			return Id.GetHashCode();
		}
	}
}